package com.design.movieticket.controller;


import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.design.movieticket.common.base.constant.Result;
import com.design.movieticket.common.base.constant.ResultStatus;
import com.design.movieticket.common.dto.XPage;
import com.design.movieticket.common.util.RandomTicketNum;
import com.design.movieticket.dto.CouponDto;
import com.design.movieticket.dto.OrdersDto;
import com.design.movieticket.entity.Coupon;
import com.design.movieticket.entity.Orders;
import com.design.movieticket.entity.Seat;
import com.design.movieticket.service.OrdersService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "订单模块")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrdersService ordersService;

    /** 分页查询 */
    @ApiOperation("订单信息分页展示")
    @GetMapping("/list/page")
    public Result findOrderListByPage(Orders orders, XPage<Void> page) {
        IPage<Orders> OrderList = ordersService.page(XPage.toIPage(page), ordersService.buildQuery(orders));
        return new Result(OrderList, ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 新增或更新订单数据 */
    @ApiOperation("新增或更新订单数据")
    @PostMapping("/addOrUpdate")
    public Result addOrEditOrder(Orders orders) {

        ordersService.addOrUpdateOrderInfo(orders);
        return new Result(ResultStatus.SAVE_SUCCESS.getMsg(),ResultStatus.SAVE_SUCCESS.getCode());
    }

    /** 获取订单信息 */
    @ApiOperation("获取订单信息详情")
    @GetMapping("/getOrderDetail/{id}")
    public Result getOrderDetail(@PathVariable long id){
        return new Result(ordersService.getOrderDetail(id),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 删除订单信息 */
    @ApiOperation("删除订单信息")
    @PostMapping("/delOrderById/{id}")
    public Result delOrderById(@PathVariable long id){
        ordersService.delOrderById(id);
        return new Result(ResultStatus.DELETE_SUCCESS.getMsg(),ResultStatus.DELETE_SUCCESS.getCode());
    }





    /** 微信订单确认 */
    @PostMapping("/wxCheckOrders")
    public Result checkOrders(@RequestBody Map<String, Object> target){
        Orders orders = JSONObject.parseObject(JSONObject.toJSONString(target.get("orders")),Orders.class);
        Long useCouponId = -1L;
        if (target.get("useCouponId") != null){
            useCouponId = JSONObject.parseObject(JSONObject.toJSONString(target.get("useCouponId")),Long.class);
        }
        return new Result(ordersService.wxCheckOrderS(orders,useCouponId),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 微信根据用户名获取现有电影票 */
    @GetMapping("/wxGetOrdersByUsername")
    public Result wxGetOrdersByUsername(String username){
        return new Result(ordersService.wxGetOrdersByUsername(username),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 微信根据用户名获取以观看电影票 */
    @GetMapping("/wxGetHadBeenMovieByUsername")
    public Result wxGetHadBeenMovieByUsername(String username){
        return new Result(ordersService.wxGetHadBeenMovieByUsername(username),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }
}
